package com.zx.idc.backend.gui.base.monitor;


import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.common.BasicBaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.common.base.BaseExcelApi;
import com.zx.idc.cache.resource.ServerResourceCacheService;
import com.zx.idc.common.exception.EncryptException;
import com.zx.idc.common.util.DealObjectNullValue;
import com.zx.idc.ds.base.monitor.entity.BaseMonitorDevice;
import com.zx.idc.ds.base.monitor.service.IBaseMonitorDeviceService;
import com.zx.idc.ds.base.monitor.vo.BaseMonitorDeviceVo;
import com.zx.idc.ds.common.constant.DelFlagConstant;
import com.zx.idc.ds.resource.pojo.ServerInfo;
import com.zx.idc.internal.exception.SendMessageException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * <p>
 * 监控设备信息管理 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-01-31
 */
@RestController
@RequestMapping("/api/baseMonitorDevice")
public class BaseMonitorDeviceController extends BasicBaseApi<IBaseMonitorDeviceService, BaseMonitorDevice> implements BaseExcelApi<IBaseMonitorDeviceService, BaseMonitorDeviceVo> {
    @Autowired
    private ServerResourceCacheService serverResourceCacheService;

    @GetMapping("/query-page")
    @RequiresPermissions("basic-data:base-monitor-device")
    public Result<Page<BaseMonitorDeviceVo>> page(Page<BaseMonitorDeviceVo> page, BaseMonitorDeviceVo baseMonitorDeviceVo) {
        return success(service.pageByParam(page, baseMonitorDeviceVo));
    }

    @GetMapping("/get-and-ip-list/{id}")
    @RequiresPermissions("basic-data:base-monitor-device")
    public Result<BaseMonitorDeviceVo> getAndIpList(@PathVariable Long id) {
        return success(service.getAndIpList(id));
    }

    /**
     * 重新获取监控设备服务器的信息
     *
     * @param host 监控设备主机名
     * @return 服务器的信息
     */
    @GetMapping("/refresh-server-info/{host}")
    @RequiresPermissions("basic-data:base-monitor-device")
    public Result<ServerInfo> refreshServerInfo(@PathVariable String host) {
        return success(serverResourceCacheService.getServerInfo(host));
    }

    /**
     * 关闭本地监控设备
     * TODO 本地监控设备不存在应用状态这个字段呀
     */
    @PostMapping("/close/{id}")
    @RequiresPermissions("basic-data:base-monitor-device:edit")
    public Result<Boolean> close(@PathVariable String id) {
        return success(true);
    }


    @PostMapping("/update-and-ip-list")
    @RequiresPermissions("basic-data:base-monitor-device:edit")
    public Result<Boolean> updateAndIpList(@RequestBody BaseMonitorDeviceVo baseMonitorDeviceVo) {
        try {
            return success(service.updateAndIpList(baseMonitorDeviceVo));
        } catch (IOException | SendMessageException e) {
            log.error("监测设备发送阻断方式失败", e);
        } catch (EncryptException e) {
            log.error("监测设备发送阻断加密方式");
        }
        return success(false);
    }

    @Override
    @RequiresPermissions("basic-data:base-monitor-device:edit")
    public Result<Boolean> add(@RequestBody BaseMonitorDevice entity) {
        return success(service.insert(entity.setDelFlag(DelFlagConstant.NORMAL)));
    }

    @Override
    @RequiresPermissions("basic-data:base-monitor-device:edit")
    public Result<Boolean> update(@RequestBody BaseMonitorDevice entity) {
        return success(service.updateAllColumnById(DealObjectNullValue.setNullValue(entity.setDelFlag(DelFlagConstant.NORMAL))));
    }


}

